//
//  ArticleManagement.m
//  reSearchDemo
//
//  Created by 科技 on 15/8/3.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "ArticleManagement.h"
#import "ArticleManagementCell.h"
#import "ArticleDetailsVC.h"
//#import "ArticleListModle.h"


@interface ArticleManagement ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_tableView;
    NSMutableArray *_dataSource;   /**< 数据源 */
    NSMutableArray *_cellArr;      /**< 存放cell,用于设置cell的高 */
}

@property (nonatomic,retain) OnePlatformModel *model;

@end

@implementation ArticleManagement

- (id)initWithPlatfromModel:(OnePlatformModel *)model {
    self = [super init];
    if (self) {
        self.model = model;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"文章管理";
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc] init];
    _tableView.sectionFooterHeight = 5.0;
    [self.view addSubview:_tableView];
    
    _dataSource = [[NSMutableArray alloc]init];
    
    [self loadModel];
    
    
}
//加载数据源
-(void)loadModel {
    
    self.loading = YES;
    NSDictionary *dicT = @{@"p_account":self.model.account};
    DLog(@"124");
    [[HTTPGlobals shareHTTPGlobal] PostHTTPRequest:@"MobilePort/index/ArticleList.html" parameters:dicT finish:^(BOOL seccess, NSString *error, id obj) {
        
        DLog(@"%@",obj);
        self.loading = NO;
        if (seccess) {
            NSNumber *code = obj[@"state"][@"code"];
            if (code.intValue != 0) {
                [self showText:obj[@"msg"]];
                return ;
            }
//            NSMutableArray *marray = [NSMutableArray array];
            NSArray *list = obj[@"list"];
//            for (NSDictionary *dic in list) {
//                ArticleListModle *model = [[ArticleListModle alloc] init];
//                [model setValuesForKeysWithDictionary:dic];
//                [_dataSource addObject:model];
//            }
            DLog(@"%@",_dataSource);
//            aClassifyList = marray;
            [_tableView reloadData];
                               }
    }];
    
    _cellArr = [NSMutableArray array];
    [_dataSource enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        ArticleManagementCell *cell = [[ArticleManagementCell alloc] init];
        [_cellArr addObject:cell];
    }];
}

#pragma mark --tableViewDelegate --
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }else {
        return _dataSource.count;
    }
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *indentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:indentifier];
    }
    if (indexPath.section == 0) {
        cell.textLabel.text = @"分类查找";
    }else {
//        ArticleListModle *model = _dataSource[indexPath.row];
//        cell.textLabel.text=model.title;
        
//时间转nsstring;
//        NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:model.time];
//        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//        [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
//        NSString * time=[dateFormatter stringFromDate:confromTimesp];
//        
//        cell.detailTextLabel.text=time;
        
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        [self showText:@"点击分类查找"];

    }else {
//        ArticleListModle *modle=_dataSource[indexPath.row];
//        [self showText:@"点击文章详情"];
//        ArticleDetailsVC *detailsVC=[[ArticleDetailsVC alloc]init];
//        detailsVC.webUrl=modle.url;
//        [self.navigationController pushViewController:detailsVC animated:YES];
        
//        [self pushViewController:[ArticleDetailsVC new]];
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 44;
}
#pragma mark --设置分区的间距--
- ( CGFloat )tableView:( UITableView *)tableView heightForHeaderInSection:( NSInteger )section {
    return 5.0 ;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
